import os

import xlwt
from organization.models import BusinessDept
from report.basemodels import ReportBase

from utils.timezone import tzinfo


class WriteExcel():
    HEADS = ['人力', '业务', '总成本', '总收入', '利润']
    NAME = '成本收入分析'

    def __init__(self, bdept:BusinessDept, save_path='.'):
        self.nrow = 0
        self.ncol = 0

        self.bdept = bdept
        self.save_path = save_path

        self.workbook = xlwt.Workbook()
        self.sheet = self.workbook.add_sheet(self.NAME, cell_overwrite_ok=True)

        self.filename = f'{self.NAME}_{self.bdept.name}.xlsx'

    def write_title(self, title):
        self.sheet.write_merge(self.nrow, self.nrow, self.ncol, self.ncol+ len(self.HEADS) - 1, title)

    def write_line(self, linedata):
        i = 0
        for head in linedata:
            self.sheet.write(self.nrow, self.ncol + i, head)
            i += 1

    def write_head(self):
        self.write_line(self.HEADS)

    def write_corner(self):
        self.sheet.write_merge(0, 1, 0, 0, '')

    def cursor_right(self, n=1):
        self.ncol += 1

    def cursor_down(self, n=1):
        self.nrow += 1

    def cursor_next_block(self):
        self.nrow = 0
        self.ncol += len(self.HEADS)

    def cursor_first_block(self):
        self.nrow = 0
        self.ncol = 1

    def gen_line(self, report:ReportBase):
        line = (report.human_cost, report.business_cost, report.total_cost, report.income, report.total_income)
        return line

    def write_block(self, obj, monthes, acc):
        self.write_title(obj.name)
        self.cursor_down()

        self.write_head()
        self.cursor_down()

        for month in monthes:
            line = self.gen_line(month)
            self.write_line(line)
            self.cursor_down()

        line = self.gen_line(acc)
        self.write_line(line)

    def write_timeline(self, timelines):
        row = 2
        col = 0

        for line in timelines:
            self.sheet.write(row, col, line)
            row += 1
        self.sheet.write(row, col, '累计')

    def write_bdept2excel(self):
        self.write_corner()
        monthes = self.bdept.businesslinemonthlyreport_set.all().values('month')
        monthes = [month['month'].astimezone(tzinfo).strftime('%y%m') for month in monthes]
        self.write_timeline(monthes)
        self.cursor_right()

        bdept_monthes = self.bdept.businesslinemonthlyreport_set.all()
        bdept_acc = self.bdept.businessdeptaccreport_set.last()

        self.cursor_first_block()
        self.write_block(self.bdept, bdept_monthes, bdept_acc)

        # 写入业务线
        for bline in self.bdept.businessline_set.all():
            self.cursor_next_block()
            bline_monthes = bline.businesslinemonthlyreport_set.all()
            bline_acc = bline.businesslineaccreport_set.last()
            self.write_block(bline, bline_monthes, bline_acc)

    def write2stream(self, stream):
        self.workbook.save(stream)

    def save(self):
        filename = os.path.join(self.save_path, self.filename)
        self.workbook.save(filename)
